# -*- coding: utf-8 -*-
from django.utils.deprecation import MiddlewareMixin
from excleapp import models

class AuthMiddleware(MiddlewareMixin):

    def process_request(self, request):
        user_id = request.session.get('user_id', 0)
         # 通过session中保存的user_id查询到用户对象
        user_obj = models.User.objects.filter(id=user_id).first()
        # 将用户对象赋值给request.login_user,模板中只需判断request.login_user即可
        request.login_user = user_obj
